package com.gator.rabbitmq.pablo_rabbitmq.simple;

import com.gator.rabbitmq.pablo_rabbitmq.utils.ConnectionUtils;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author PABLO
 * @Date 2022/6/4 21:41
 * @Desc 生产者
 */
public class SimpleQueueProducer {

    public static final String QUEUE_NAME = "hello";


    public static void main(String[] args) {

        Channel channel = ConnectionUtils.getConnectionAndChannel();
        try {

            //设置队列优先级
            Map<String, Object> arguments = new HashMap<>();
            //系统支持0-255范围
            arguments.put("x-max-priority", 10);

            //队列名称
            //是否持久化
            //消费者是否独占
            //最后一个消费者断开连接后，该队列是否自动删除
            //其他参数
            channel.queueDeclare(QUEUE_NAME, false,
                    false, false,
                    arguments);
            //发消息
            //指定交换机
            //路由key,对应某个队列
            //其他参数
            //发送的消息
            for (int i = 1; i <= 100; i++) {
                if (i == 50) {
                    //设置优先级
                    AMQP.BasicProperties properties = new AMQP.BasicProperties().builder().priority(9).build();
                    channel.basicPublish("", QUEUE_NAME, properties, ("hello_world" + i).getBytes(StandardCharsets.UTF_8));

                } else if (i == 80) {
                    AMQP.BasicProperties properties = new AMQP.BasicProperties().builder().priority(8).build();
                    channel.basicPublish("", QUEUE_NAME, properties, ("hello_world" + i).getBytes(StandardCharsets.UTF_8));
                } else {
                    channel.basicPublish("", QUEUE_NAME, null, ("hello_world" + i).getBytes(StandardCharsets.UTF_8));
                }
                System.out.println("send success!");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
